package com.example.myapplication6;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import java.util.Calendar;
import java.util.Date;

public class Step2Fragment extends Fragment {
    private Order order;
    private TextView tvDate;

    public void setOrder(Order order) {
        this.order = order;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_step, container, false);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        LinearLayout llContent = view.findViewById(R.id.ll_content);
        Button btnNext = view.findViewById(R.id.btn_next);

        tvTitle.setText("步骤2：选择乘车日期");

        // 添加日期选择文本
        tvDate = new TextView(getContext());
        tvDate.setText("点击选择日期");
        tvDate.setTextSize(16);
        tvDate.setOnClickListener(v -> showDatePicker());
        llContent.addView(tvDate);

        // 下一步
        btnNext.setOnClickListener(v -> ((MainActivity) getActivity()).goToStep(2));

        return view;
    }

    // 原生日期选择器
    private void showDatePicker() {
        Calendar calendar = Calendar.getInstance();
        new DatePickerDialog(
                getContext(),
                (view, year, month, day) -> {
                    calendar.set(year, month, day);
                    order.rideDate = calendar.getTime(); // 保存日期
                    tvDate.setText("已选：" + year + "-" + (month+1) + "-" + day);
                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)
        ).show();
    }
}